package com.ship.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.dispatch.bean.SpYearPlanScheme;
import com.ship.dispatch.bean.SpYearPlanSchemeDetail;
import com.ship.dispatch.bean.SpYearPlanSchemeDetailHistory;
import com.ship.dispatch.bo.PlanSchemeDetailHistorysStatusBO;
import com.ship.dispatch.bo.ReduceProgrammeBO;
import com.ship.dispatch.bo.SpYearPlanSchemeDetailHistoryBO;
import com.ship.dispatch.mapper.SpYearPlanSchemeDetailHistoryMapper;
import com.ship.dispatch.mapper.SpYearPlanSchemeDetailMapper;
import com.ship.dispatch.mapper.SpYearPlanSchemeMapper;
import com.ship.dispatch.service.SpYearPlanSchemeDetailHistoryService;
import com.ship.dispatch.util.IDUtil;
import com.ship.dispatch.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class SpYearPlanSchemeDetailServiceHistoryImpl extends BaseServiceImpl<SpYearPlanSchemeDetailHistoryMapper, SpYearPlanSchemeDetailHistory> implements SpYearPlanSchemeDetailHistoryService {

    @Autowired
    private SpYearPlanSchemeDetailHistoryMapper spYearPlanSchemeDetailHistoryMapper;

    @Autowired
    private SpYearPlanSchemeDetailMapper spYearPlanSchemeDetailMapper;

    @Autowired
    private SpYearPlanSchemeMapper yearPlanSchemeMapper;


    /**
     * 还原方案
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public Long reduceProgrammeBySchemeId(ReduceProgrammeBO reduceProgrammeBO) {
        AtomicReference<Long> planId = new AtomicReference<>();
        QueryWrapper<SpYearPlanSchemeDetailHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheme_id", reduceProgrammeBO.getSchemeId());
        if (StringUtils.isNotBlank(reduceProgrammeBO.getCreateBy())) {
            queryWrapper.eq("create_by", reduceProgrammeBO.getCreateBy());
        }
        List<SpYearPlanSchemeDetailHistory> histories = spYearPlanSchemeDetailHistoryMapper.selectList(queryWrapper);
        if (histories != null && histories.size() > 0) {
            histories.stream().forEach(p -> {
                planId.set(p.getPlanId());
                SpYearPlanSchemeDetail spYearPlanSchemeDetail = spYearPlanSchemeDetailMapper.selectById(p.getSchemeDetailId());
                if (spYearPlanSchemeDetail != null) {
                    SpYearPlanSchemeDetail schemeDetail = new SpYearPlanSchemeDetail();
                    BeanUtils.copyProperties(p, schemeDetail);
                    schemeDetail.setId(IDUtil.nextId());
                    int num = spYearPlanSchemeDetailMapper.insert(schemeDetail);
                    if (num > 0) {
                        spYearPlanSchemeDetailMapper.deleteById(spYearPlanSchemeDetail);
                        SpYearPlanScheme scheme = yearPlanSchemeMapper.selectById(reduceProgrammeBO.getSchemeId());
                        if (scheme != null) {
                            scheme.setIsReduce(1);
                            scheme.setReduceDate(new Date());
                            yearPlanSchemeMapper.updateById(scheme);
                        }
                    }
                }
            });
        }
        return planId.get();
    }

    /**
     * 新增历史记录
     *
     * @return
     */
    @Override
    public void savePlanSchemeDetailHistory(SpYearPlanSchemeDetailHistoryBO spYearPlanSchemeDetailHistoryBO) {
        //处理手动排布，船舶上下拖拽，不管拖拽的信息长度这么变动，只要拖拽都放入集合formAndTOBOS中,所有手动都不要存放到details中
        if (spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS() != null && spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS().size() > 0) {
            spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS().stream().forEach(p -> {
                QueryWrapper<SpYearPlanSchemeDetail> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("scheme_detail_id", p.getSchemeId());
                queryWrapper.eq("imo", p.getToImo());
                if (StringUtils.isNotBlank(p.getCreateBy())) {
                    queryWrapper.eq("create_by", p.getCreateBy());
                }
                SpYearPlanSchemeDetail spYearPlanSchemeDetailFrom = spYearPlanSchemeDetailMapper.selectById(p.getFromId());
                SpYearPlanSchemeDetail spYearPlanSchemeDetailTo = spYearPlanSchemeDetailMapper.selectOne(queryWrapper);

                SpYearPlanSchemeDetail spYearPlanSchemeDetailNew = new SpYearPlanSchemeDetail();
                BeanUtils.copyProperties(spYearPlanSchemeDetailFrom, spYearPlanSchemeDetailNew);
                spYearPlanSchemeDetailNew.setId(IDUtil.nextId());
                spYearPlanSchemeDetailNew.setGoodsNum(spYearPlanSchemeDetailTo.getGoodsNum());
                spYearPlanSchemeDetailNew.setResourceContract(spYearPlanSchemeDetailTo.getResourceContract());
                spYearPlanSchemeDetailNew.setContractName(spYearPlanSchemeDetailTo.getContractName());
                spYearPlanSchemeDetailNew.setSeller(spYearPlanSchemeDetailTo.getSeller());
                spYearPlanSchemeDetailNew.setTradeType(spYearPlanSchemeDetailTo.getTradeType());
                spYearPlanSchemeDetailNew.setTradeBody(spYearPlanSchemeDetailTo.getTradeBody());
                spYearPlanSchemeDetailNew.setShipName(spYearPlanSchemeDetailTo.getShipName());
                spYearPlanSchemeDetailNew.setImo(spYearPlanSchemeDetailTo.getImo());
                spYearPlanSchemeDetailNew.setPortChangeDateOne(spYearPlanSchemeDetailTo.getPortChangeDateOne());
                spYearPlanSchemeDetailNew.setPortChangeDateTwo(spYearPlanSchemeDetailTo.getPortChangeDateTwo());
                spYearPlanSchemeDetailNew.setShipChangeDateOne(spYearPlanSchemeDetailTo.getShipChangeDateOne());
                spYearPlanSchemeDetailNew.setShipChangeDateTwo(spYearPlanSchemeDetailTo.getShipChangeDateTwo());
                spYearPlanSchemeDetailNew.setLoadingCapacity(spYearPlanSchemeDetailTo.getLoadingCapacity());
                spYearPlanSchemeDetailNew.setShipSpeed(spYearPlanSchemeDetailTo.getShipSpeed());
                spYearPlanSchemeDetailNew.setQuantityUnit(spYearPlanSchemeDetailTo.getQuantityUnit());
                spYearPlanSchemeDetailNew.setSingleGoodsQuantity(spYearPlanSchemeDetailTo.getSingleGoodsQuantity());
                int num = spYearPlanSchemeDetailMapper.insert(spYearPlanSchemeDetailNew);
                if (num > 0) {
                    spYearPlanSchemeDetailMapper.deleteById(spYearPlanSchemeDetailFrom.getId());
                }
            });
        }

        if (spYearPlanSchemeDetailHistoryBO.getDetails() != null && spYearPlanSchemeDetailHistoryBO.getDetails().size() > 0) {
            spYearPlanSchemeDetailHistoryBO.getDetails().stream().forEach(s -> {
                SpYearPlanSchemeDetailHistory spYearPlanSchemeDetailHistory = new SpYearPlanSchemeDetailHistory();
                BeanUtils.copyProperties(s, spYearPlanSchemeDetailHistory);
                spYearPlanSchemeDetailHistory.setId(IDUtil.nextId());
                spYearPlanSchemeDetailHistory.setHistoryUserDate(new Date());
                spYearPlanSchemeDetailHistory.setSchemeDetailId(s.getId());
                spYearPlanSchemeDetailHistoryMapper.insert(spYearPlanSchemeDetailHistory);
            });
        }
    }

    @Override
    public void savePlanSchemeDetailHistory(SpYearPlanSchemeDetailHistoryBO spYearPlanSchemeDetailHistoryBO, Long schemeOldId,Long schemeNewId) {
        //处理手动排布，船舶上下拖拽，不管拖拽的信息长度这么变动，只要拖拽都放入集合formAndTOBOS中,所有手动都不要存放到details中
        if (spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS() != null && spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS().size() > 0) {
            spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS().stream().forEach(p -> {
                LambdaQueryWrapper<SpYearPlanSchemeDetail> lambdaQueryWrapper = Wrappers.lambdaQuery();

                lambdaQueryWrapper.eq(SpYearPlanSchemeDetail::getSchemeId, schemeOldId);
                lambdaQueryWrapper.eq(SpYearPlanSchemeDetail::getImo, p.getToImo());
                if (StringUtils.isNotBlank(p.getCreateBy())) {
                    lambdaQueryWrapper.eq(SpYearPlanSchemeDetail::getCreateBy, p.getCreateBy());
                }
                AtomicReference<SpYearPlanSchemeDetailVO> spYearPlanSchemeDetailFrom = new AtomicReference<>(new SpYearPlanSchemeDetailVO());
                //SpYearPlanSchemeDetail spYearPlanSchemeDetailFrom = spYearPlanSchemeDetailMapper.selectById(p.getFromId());
                if(spYearPlanSchemeDetailHistoryBO.getDetails()!=null && spYearPlanSchemeDetailHistoryBO.getDetails().size()>0){
                    spYearPlanSchemeDetailHistoryBO.getDetails().stream().forEach(o->{
                        if(o.getId().equals(p.getFromId())){
                            spYearPlanSchemeDetailFrom.set(o);
                        }
                    });
                }
                SpYearPlanSchemeDetail spYearPlanSchemeDetailTo = spYearPlanSchemeDetailMapper.selectOne(lambdaQueryWrapper);
                SpYearPlanSchemeDetailHistory historyModel = new SpYearPlanSchemeDetailHistory();

                SpYearPlanSchemeDetailHistory spYearPlanSchemeDetailNew = new SpYearPlanSchemeDetailHistory();
                if(null != spYearPlanSchemeDetailFrom.get()){
                    BeanUtils.copyProperties(spYearPlanSchemeDetailFrom.get(), spYearPlanSchemeDetailNew);
                }else{
                    //history:查询移动前的数据,这个条件永远不会走
                    SpYearPlanSchemeDetailHistory history = spYearPlanSchemeDetailHistoryMapper.selectById(p.getFromId());
                    BeanUtils.copyProperties(history, spYearPlanSchemeDetailNew);
                }

                if(null == spYearPlanSchemeDetailTo){
                    LambdaQueryWrapper<SpYearPlanSchemeDetailHistory> lambdaHistoryQueryWrapper = Wrappers.lambdaQuery();
                    lambdaHistoryQueryWrapper.eq(SpYearPlanSchemeDetailHistory::getSchemeId, schemeOldId);
                    lambdaHistoryQueryWrapper.eq(SpYearPlanSchemeDetailHistory::getImo, p.getToImo());
                    if (StringUtils.isNotBlank(p.getCreateBy())) {
                        lambdaHistoryQueryWrapper.eq(SpYearPlanSchemeDetailHistory::getCreateBy, p.getCreateBy());
                    }
//                    if (null !=p.getFromId()) {
//                        lambdaHistoryQueryWrapper.eq(SpYearPlanSchemeDetailHistory::getId, p.getFromId());
//                    }
                   List<SpYearPlanSchemeDetailHistory>  historyModels = spYearPlanSchemeDetailHistoryMapper.selectList(lambdaHistoryQueryWrapper);
                    if(historyModels!=null && historyModels.size()>0){
                        historyModel = historyModels.get(0);
                    }
                     // historyModel = spYearPlanSchemeDetailHistoryMapper.selectOne(lambdaHistoryQueryWrapper);
                }else{
                    spYearPlanSchemeDetailNew.setShipName(spYearPlanSchemeDetailTo.getShipName());
                    spYearPlanSchemeDetailNew.setImo(spYearPlanSchemeDetailTo.getImo());
//                    spYearPlanSchemeDetailNew.setResourceContract(spYearPlanSchemeDetailTo.getResourceContract());
//                    spYearPlanSchemeDetailNew.setContractName(spYearPlanSchemeDetailTo.getShipContractName());
//                    spYearPlanSchemeDetailNew.setSeller(spYearPlanSchemeDetailTo.getSeller());
//                    spYearPlanSchemeDetailNew.setTradeType(spYearPlanSchemeDetailTo.getTradeType());
//                    spYearPlanSchemeDetailNew.setTradeBody(spYearPlanSchemeDetailTo.getTradeBody());
//                    spYearPlanSchemeDetailNew.setPortChangeDateOne(spYearPlanSchemeDetailTo.getPortChangeDateOne());
//                    spYearPlanSchemeDetailNew.setPortChangeDateTwo(spYearPlanSchemeDetailTo.getPortChangeDateTwo());
//                    spYearPlanSchemeDetailNew.setShipChangeDateOne(spYearPlanSchemeDetailTo.getShipChangeDateOne());
//                    spYearPlanSchemeDetailNew.setShipChangeDateTwo(spYearPlanSchemeDetailTo.getShipChangeDateTwo());
//                    spYearPlanSchemeDetailNew.setLoadingCapacity(spYearPlanSchemeDetailTo.getLoadingCapacity());
//                    spYearPlanSchemeDetailNew.setShipSpeed(spYearPlanSchemeDetailTo.getShipSpeed());
//                    spYearPlanSchemeDetailNew.setQuantityUnit(spYearPlanSchemeDetailTo.getQuantityUnit());
//                    spYearPlanSchemeDetailNew.setSingleGoodsQuantity(spYearPlanSchemeDetailTo.getSingleGoodsQuantity());
                }

                spYearPlanSchemeDetailNew.setId(IDUtil.nextId());
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getGoodsNum()){
//                        spYearPlanSchemeDetailNew.setGoodsNum(historyModel.getGoodsNum());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getGoodsNum()){
//                        spYearPlanSchemeDetailNew.setGoodsNum(spYearPlanSchemeDetailTo.getGoodsNum());
//                    }
//                }

//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getResourceContract()){
//                        spYearPlanSchemeDetailNew.setResourceContract(historyModel.getResourceContract());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getResourceContract()){
//                        spYearPlanSchemeDetailNew.setResourceContract(spYearPlanSchemeDetailTo.getResourceContract());
//                    }
//                }

//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getContractName()){
//                        spYearPlanSchemeDetailNew.setContractName(historyModel.getContractName());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getContractName()){
//                        spYearPlanSchemeDetailNew.setContractName(spYearPlanSchemeDetailTo.getContractName());
//                    }
//                }

//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getSeller()){
//                        spYearPlanSchemeDetailNew.setSeller(historyModel.getSeller());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getSeller()){
//                        spYearPlanSchemeDetailNew.setSeller(spYearPlanSchemeDetailTo.getSeller());
//                    }
//                }

//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getTradeType()){
//                        spYearPlanSchemeDetailNew.setTradeType(historyModel.getTradeType());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getTradeType()){
//                        spYearPlanSchemeDetailNew.setTradeType(spYearPlanSchemeDetailTo.getTradeType());
//                    }
//                }
//
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getTradeBody()){
//                        spYearPlanSchemeDetailNew.setTradeBody(historyModel.getTradeBody());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getTradeBody()){
//                        spYearPlanSchemeDetailNew.setTradeBody(spYearPlanSchemeDetailTo.getTradeBody());
//                    }
//                }

                if(null == spYearPlanSchemeDetailTo){
                    if(null != historyModel && null != historyModel.getShipName()){
                        spYearPlanSchemeDetailNew.setShipName(historyModel.getShipName());
                    }
                }else{
                    if(null !=spYearPlanSchemeDetailTo.getShipName()){
                        spYearPlanSchemeDetailNew.setShipName(spYearPlanSchemeDetailTo.getShipName());
                    }
                }

                if(null == spYearPlanSchemeDetailTo){
                    if(null != historyModel && null != historyModel.getImo()){
                        spYearPlanSchemeDetailNew.setImo(historyModel.getImo());
                    }
                }else{
                    if(null !=spYearPlanSchemeDetailTo.getImo()){
                        spYearPlanSchemeDetailNew.setImo(spYearPlanSchemeDetailTo.getImo());
                    }
                }
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getPortChangeDateOne()){
//                        spYearPlanSchemeDetailNew.setPortChangeDateOne(historyModel.getPortChangeDateOne());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getPortChangeDateOne()){
//                        spYearPlanSchemeDetailNew.setPortChangeDateOne(spYearPlanSchemeDetailTo.getPortChangeDateOne());
//                    }
//                }
//
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getPortChangeDateTwo()){
//                        spYearPlanSchemeDetailNew.setPortChangeDateTwo(historyModel.getPortChangeDateTwo());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getPortChangeDateTwo()){
//                        spYearPlanSchemeDetailNew.setPortChangeDateTwo(spYearPlanSchemeDetailTo.getPortChangeDateTwo());
//                    }
//                }
//
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getShipChangeDateOne()){
//                        spYearPlanSchemeDetailNew.setShipChangeDateOne(historyModel.getShipChangeDateOne());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getShipChangeDateOne()){
//                        spYearPlanSchemeDetailNew.setShipChangeDateOne(spYearPlanSchemeDetailTo.getShipChangeDateOne());
//                    }
//                }
//
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getShipChangeDateTwo()){
//                        spYearPlanSchemeDetailNew.setShipChangeDateTwo(historyModel.getShipChangeDateTwo());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getShipChangeDateTwo()){
//                        spYearPlanSchemeDetailNew.setShipChangeDateTwo(spYearPlanSchemeDetailTo.getShipChangeDateTwo());
//                    }
//                }
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getLoadingCapacity()){
//                        spYearPlanSchemeDetailNew.setLoadingCapacity(historyModel.getLoadingCapacity());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getLoadingCapacity()){
//                        spYearPlanSchemeDetailNew.setLoadingCapacity(spYearPlanSchemeDetailTo.getLoadingCapacity());
//                    }
//                }
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getShipSpeed()){
//                        spYearPlanSchemeDetailNew.setShipSpeed(historyModel.getShipSpeed());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getShipSpeed()){
//                        spYearPlanSchemeDetailNew.setShipSpeed(spYearPlanSchemeDetailTo.getShipSpeed());
//                    }
//                }
//
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getQuantityUnit()){
//                        spYearPlanSchemeDetailNew.setQuantityUnit(historyModel.getQuantityUnit());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getQuantityUnit()){
//                        spYearPlanSchemeDetailNew.setQuantityUnit(spYearPlanSchemeDetailTo.getQuantityUnit());
//                    }
//                }
//
//                if(null == spYearPlanSchemeDetailTo){
//                    if(null != historyModel && null != historyModel.getSingleGoodsQuantity()){
//                        spYearPlanSchemeDetailNew.setSingleGoodsQuantity(historyModel.getSingleGoodsQuantity());
//                    }
//                }else{
//                    if(null !=spYearPlanSchemeDetailTo.getSingleGoodsQuantity()){
//                        spYearPlanSchemeDetailNew.setSingleGoodsQuantity(spYearPlanSchemeDetailTo.getSingleGoodsQuantity());
//                    }
//                }
                spYearPlanSchemeDetailNew.setCreateBy(p.getCreateBy());
                spYearPlanSchemeDetailNew.setSchemeId(schemeNewId);
                //保留最初方案id一直不变
                if(spYearPlanSchemeDetailNew.getSchemeOldId()==null){
                    LambdaQueryWrapper<SpYearPlanSchemeDetail> oldSchemeOldWrapper = Wrappers.lambdaQuery();
                    oldSchemeOldWrapper.eq(SpYearPlanSchemeDetail::getId, p.getFromId());
                    SpYearPlanSchemeDetail schemeDetail = spYearPlanSchemeDetailMapper.selectOne(oldSchemeOldWrapper);
                    if(schemeDetail!=null){
                        spYearPlanSchemeDetailNew.setSchemeOldId(schemeDetail.getSchemeId());
                        spYearPlanSchemeDetailNew.setSchemeDetailId(p.getFromId());
                    }
                }
                spYearPlanSchemeDetailHistoryMapper.insert(spYearPlanSchemeDetailNew);
            });
        }

        if (spYearPlanSchemeDetailHistoryBO.getDetails() != null && spYearPlanSchemeDetailHistoryBO.getDetails().size() > 0) {
            spYearPlanSchemeDetailHistoryBO.getDetails().stream().forEach(s -> {
                AtomicBoolean judge = new AtomicBoolean(false);
                if(spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS()!=null && spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS().size()>0){
                    spYearPlanSchemeDetailHistoryBO.getFormAndTOBOS().stream().forEach(p->{
                       if(p.getFromId().equals(s.getId())){
                           judge.set(true);
                           //设置一个空的船名，作用：空船名显示甘特图
                           SpYearPlanSchemeDetailHistory spYearPlanSchemeDetailHistory = new SpYearPlanSchemeDetailHistory();
                           spYearPlanSchemeDetailHistory.setSchemeId(schemeNewId);
                           spYearPlanSchemeDetailHistory.setPlanId(s.getPlanId());
                           spYearPlanSchemeDetailHistory.setCreateBy(p.getCreateBy());
                           spYearPlanSchemeDetailHistory.setId(IDUtil.nextId());
                           spYearPlanSchemeDetailHistory.setShipName(s.getShipName());
                           spYearPlanSchemeDetailHistory.setImo(s.getImo());
                           //保留最初方案id一直不变
                           if(s.getSchemeOldId()==null){
                               LambdaQueryWrapper<SpYearPlanSchemeDetail> oldSchemeOldWrapper = Wrappers.lambdaQuery();
                               oldSchemeOldWrapper.eq(SpYearPlanSchemeDetail::getId, p.getFromId());
                               SpYearPlanSchemeDetail schemeDetail = spYearPlanSchemeDetailMapper.selectOne(oldSchemeOldWrapper);
                               if(schemeDetail!=null){
                                   spYearPlanSchemeDetailHistory.setSchemeOldId(schemeDetail.getSchemeId());
                               }
                           }
                           spYearPlanSchemeDetailHistoryMapper.insert(spYearPlanSchemeDetailHistory);
                       }
                    });
                }
                if(!judge.get()){
                    SpYearPlanSchemeDetailHistory spYearPlanSchemeDetailHistory = new SpYearPlanSchemeDetailHistory();
                    BeanUtils.copyProperties(s, spYearPlanSchemeDetailHistory);
                    spYearPlanSchemeDetailHistory.setId(IDUtil.nextId());
                    spYearPlanSchemeDetailHistory.setHistoryUserDate(new Date());
                    spYearPlanSchemeDetailHistory.setCreateBy(s.getCreateBy());
                    spYearPlanSchemeDetailHistory.setSchemeId(schemeNewId);
                    //保留最初方案id一直不变,每条方案详情id一直不变
                    if(s.getSchemeOldId()==null){
                        LambdaQueryWrapper<SpYearPlanSchemeDetail> oldSchemeOldWrapper = Wrappers.lambdaQuery();
                        oldSchemeOldWrapper.eq(SpYearPlanSchemeDetail::getId, s.getId());
                        SpYearPlanSchemeDetail schemeDetail = spYearPlanSchemeDetailMapper.selectOne(oldSchemeOldWrapper);
                        if(schemeDetail!=null){
                            spYearPlanSchemeDetailHistory.setSchemeOldId(schemeDetail.getSchemeId());
                            spYearPlanSchemeDetailHistory.setSchemeDetailId(schemeDetail.getId());
                        }
                    }
                    spYearPlanSchemeDetailHistoryMapper.insert(spYearPlanSchemeDetailHistory);
                }
            });
        }
    }


    /**
     * 查询历史记录（通过schemeDetailId）
     *
     * @return
     */
    @Override
    public List<SpYearPlanSchemeHistoryVO> getPlanSchemeDetailHistorys(PlanSchemeDetailHistorysStatusBO historyStatusBO) {
        List<SpYearPlanSchemeHistoryVO> histories = new ArrayList<>();
        if (historyStatusBO.getStatus().equals(1)) {
            List<SpYearPlanScheme> planSchemes = yearPlanSchemeMapper.getYearPlanSchemeByDraft(historyStatusBO.getCreateBy(), historyStatusBO.getSchemeDetailId(), historyStatusBO.getSchemeId());
            if(planSchemes!=null && planSchemes.size()>0){
                planSchemes.stream().forEach(s->{
                    SpYearPlanSchemeHistoryVO historyVO = new SpYearPlanSchemeHistoryVO();
                    BeanUtils.copyProperties(s,historyVO);
                    List<SpYearPlanSchemeDetailHistoryVO> historyVOS = spYearPlanSchemeDetailHistoryMapper.getDraftHistory(historyStatusBO.getCreateBy(), historyStatusBO.getSchemeDetailId(), historyStatusBO.getSchemeId(),s.getId());
                    historyVO.setHistorys(historyVOS);
                    histories.add(historyVO);
                });
            }
        } else if (historyStatusBO.getStatus().equals(2)) {
            List<SpYearPlanScheme> planSchemes = yearPlanSchemeMapper.getYearPlanSchemeByShare(historyStatusBO.getCreateBy(), historyStatusBO.getSchemeDetailId(), historyStatusBO.getSchemeId());
            if(planSchemes!=null && planSchemes.size()>0){
                planSchemes.stream().forEach(s->{
                    SpYearPlanSchemeHistoryVO historyVO = new SpYearPlanSchemeHistoryVO();
                    BeanUtils.copyProperties(s,historyVO);
                    List<SpYearPlanSchemeDetailHistoryVO> historyVOS = spYearPlanSchemeDetailHistoryMapper.getShareHistory(historyStatusBO.getCreateBy(), historyStatusBO.getSchemeDetailId(), historyStatusBO.getSchemeId(),s.getId());
                    historyVO.setHistorys(historyVOS);
                    histories.add(historyVO);
                });
            }
        }
        return histories;
    }

    /**
     * 尝试排布
     *
     * @return
     */
    public List<PlanDetailsBySchemeHistoryVO> getTryToArrange(Integer year, String createBy) {
        List<PlanDetailsBySchemeHistoryVO> historyVOS = new ArrayList<>();

        QueryWrapper<SpYearPlanScheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_by", createBy);
        List<SpYearPlanScheme> planSchemes = yearPlanSchemeMapper.selectList(queryWrapper);
        if (planSchemes != null && planSchemes.size() > 0) {
            planSchemes.stream().forEach(p -> {
                QueryWrapper<SpYearPlanSchemeDetailHistory> planSchemeDetailHistoryWrapper = new QueryWrapper<>();
                planSchemeDetailHistoryWrapper.eq("scheme_id", p.getId());
                List<SpYearPlanSchemeDetailHistory> judgeNum = spYearPlanSchemeDetailHistoryMapper.selectList(planSchemeDetailHistoryWrapper);
                if (judgeNum != null && judgeNum.size() > 0) {
                    PlanDetailsBySchemeHistoryVO planDetailsBySchemeHistoryVO = new PlanDetailsBySchemeHistoryVO();
                    planDetailsBySchemeHistoryVO.setSchemeId(p.getId());
                    planDetailsBySchemeHistoryVO.setShip(p.getShip());
                    planDetailsBySchemeHistoryVO.setDays(p.getDays());
                    planDetailsBySchemeHistoryVO.setTrade(p.getTrade());
                    planDetailsBySchemeHistoryVO.setFreight(p.getFreight());
                    planDetailsBySchemeHistoryVO.setCreateDate(p.getCreateDate());

                    PlanDetailsByModelNameHistory history = new PlanDetailsByModelNameHistory();
                    //构建船舶集合
                    List<String> shipNames = spYearPlanSchemeDetailHistoryMapper.getPlanDetailsHistoryByShipName(createBy);
                    if (shipNames != null && shipNames.size() > 0) {
                        shipNames.stream().forEach(s -> {
                            PlanDetailsByShipNameHistoryVO planDetailsByShipName = new PlanDetailsByShipNameHistoryVO();
                            planDetailsByShipName.setShipName(s);
                            List<SpYearPlanSchemeDetailHistoryVO> detailsVo = new ArrayList<>();
                            detailsVo = spYearPlanSchemeDetailHistoryMapper.getShipTryToArrange(year, createBy, s, p.getId());
                            if (detailsVo.size() > 0) {
                                planDetailsByShipName.setDetails(detailsVo);
                                history.getShipNames().add(planDetailsByShipName);
                            }
                        });
                    }
                    //构建货源集合
                    List<String> goodsNames = spYearPlanSchemeDetailHistoryMapper.getPlanDetailsHistoryByGoodsName(createBy);
                    if (goodsNames != null && goodsNames.size() > 0) {
                        goodsNames.stream().forEach(s -> {
                            PlanDetailsByGoodsNameHistoryVO planDetailsByGoodsName = new PlanDetailsByGoodsNameHistoryVO();
                            planDetailsByGoodsName.setGoodsName(s);
                            List<SpYearPlanSchemeDetailHistoryVO> detailsVo = new ArrayList<>();
                            detailsVo = spYearPlanSchemeDetailHistoryMapper.getGoodsNumTryToArrange(year, createBy, s, p.getId());
                            if (detailsVo.size() > 0) {
                                planDetailsByGoodsName.setDetails(detailsVo);
                                history.getGoodsNames().add(planDetailsByGoodsName);
                            }
                        });
                    }
                    //构建资源方集合
                    List<String> sellerNames = spYearPlanSchemeDetailHistoryMapper.getPlanDetailsHistoryBySellerName(createBy);
                    if (sellerNames != null && sellerNames.size() > 0) {
                        sellerNames.stream().forEach(s -> {
                            PlanDetailsBySellerNameHistoryVO planDetailsBySellerName = new PlanDetailsBySellerNameHistoryVO();
                            planDetailsBySellerName.setSellerName(s);
                            List<SpYearPlanSchemeDetailHistoryVO> detailsVo = new ArrayList<>();
                            detailsVo = spYearPlanSchemeDetailHistoryMapper.getSellerTryToArrange(year, createBy, s, p.getId());
                            if (detailsVo.size() > 0) {
                                planDetailsBySellerName.setDetails(detailsVo);
                                history.getSellerNames().add(planDetailsBySellerName);
                            }
                        });
                    }
                    planDetailsBySchemeHistoryVO.setScheme(history);
                    historyVOS.add(planDetailsBySchemeHistoryVO);
                }
            });
        }
        return historyVOS;
    }

    @Override
    public List<String> getPlanDetailsHistoryByShipName(String createBy) {
        return spYearPlanSchemeDetailHistoryMapper.getPlanDetailsHistoryByShipName(createBy);
    }

    @Override
    public List<String> getPlanDetailsHistoryByGoodsName(String createBy) {
        return spYearPlanSchemeDetailHistoryMapper.getPlanDetailsHistoryByGoodsName(createBy);
    }

    @Override
    public List<String> getPlanDetailsHistoryBySellerName(String createBy) {
        return spYearPlanSchemeDetailHistoryMapper.getPlanDetailsHistoryBySellerName(createBy);
    }

    @Override
    public List<SpYearPlanSchemeDetailHistoryVO> getShipTryToArrange(Integer year, String createBy, String shipName, Long schemeId) {
        return spYearPlanSchemeDetailHistoryMapper.getShipTryToArrange(year,createBy,shipName,schemeId);
    }

    @Override
    public List<SpYearPlanSchemeDetailHistoryVO> getGoodsNumTryToArrange(Integer year, String createBy, String goodsNum, Long schemeId) {
        return spYearPlanSchemeDetailHistoryMapper.getGoodsNumTryToArrange(year,createBy,goodsNum,schemeId);
    }

    @Override
    public List<SpYearPlanSchemeDetailHistoryVO> getSellerTryToArrange(Integer year, String createBy, String seller, Long schemeId) {
        return spYearPlanSchemeDetailHistoryMapper.getSellerTryToArrange(year,createBy,seller,schemeId);
    }
}
